package com.busibuilding.administrate.area.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.busibuilding.administrate.area.dao.AreaDAO;
import com.busibuilding.administrate.area.vo.AreaCVO;
import com.busibuilding.common.exceptions.DAOException;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.common.services.CommonServicesImpl;
import com.busibuilding.common.vo.ICommonVO;

/**
 * Class Name: AreaServiceImpl.java
 * Description: 
 * @author Lin Yanjun
 * @version 1.0
 * @since 1.0
 * Date: 2010-3-3
 */

@Transactional(rollbackForClassName={"Exception"})
public class AreaServiceImpl extends CommonServicesImpl implements AreaService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 690138188085135342L;

	private static Logger logger = LogManager.getLogger(AreaServiceImpl.class);
	
	private AreaDAO dao;

	public void setDao(AreaDAO dao) {
		this.dao = dao;
	}
	@Override
	public int getRecordsCount(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.getRecordsCount(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_AREA, e);
		}
		return id;
	}

	@Override
	public int createArea(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.insert(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_AREA, e);
		}
		return id;
	}

	@Override
	public int deleteArea(ICommonVO vo) throws ServiceException {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public AreaCVO getArea(ICommonVO vo) throws ServiceException {
		List<AreaCVO> resultList = new ArrayList<AreaCVO>();
		try {
			resultList = dao.searchAreaByCondition(vo);			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		}
		if(resultList.size()>0){
			return resultList.get(0);
		}
		return null;
	}
	
	@Override
	public List<AreaCVO> searchArea(ICommonVO vo) throws ServiceException {
		List<AreaCVO> resultList = new ArrayList<AreaCVO>();
		try {
			resultList = dao.searchAreaByCondition(vo);			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		}
		return resultList;
	}

	@Override
	public List<AreaCVO> searchAreaByPagination(ICommonVO vo)
			throws ServiceException {
		List<AreaCVO> resultList = new ArrayList<AreaCVO>();
		try {
			resultList = dao.searchAreaByCondition(vo, vo.getMinIndex(), vo.getMaxIndex());			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_AREA, e);
		}
		return resultList;
	}

	@Override
	public int updateArea(ICommonVO vo) throws ServiceException {
		int result = SQLMAP_COMMON_RESULT_ERROR;
		try {
			result = dao.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_AREA, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_AREA, e);
		}
		return result;
	}
	

}
